package com.kuanter.kuanterauto.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.db.DBManager;
import com.kuanter.kuanterauto.wxapi.WXEntryActivity;
import com.umeng.analytics.MobclickAgent;

public class MyMessageDetailActivity extends Activity {
	private TextView message_title_tv;
	private TextView message_push_time;
	private TextView message_content;
	private TextView delete_message;
	private String sqlID;
	private Button back;
	private String position;
	public  DBManager dBManager;
	private ImageView share;
	private String pushTime;
	private int index;

	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	    MerchantTurnActivity1.sh = false;
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onDestroy() {
		MerchantTurnActivity1.sh = false;
		super.onDestroy();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_message_detail);
		
		position = getIntent().getStringExtra("position");
		sqlID = getIntent().getStringExtra("sqlID");
		String title = getIntent().getStringExtra("title");
		pushTime = getIntent().getStringExtra("pushTime");
		String content = getIntent().getStringExtra("content");
		message_title_tv =(TextView)this.findViewById(R.id.message_title);
		message_title_tv.setText(title);
		message_push_time = (TextView)this.findViewById(R.id.message_push_time);
		index = pushTime.lastIndexOf("-") +3 ;
//		System.out.println(pushTime.substring(0, index).replaceAll("-", "/"));
//		msg_date_tv.setText(pushTime.substring(0, index).replaceAll("-", "/"));
		message_push_time.setText(pushTime.substring(0, index).replaceAll("-", "/"));
		message_content = (TextView)this.findViewById(R.id.message_content);
		message_content.setText(content);
		delete_message = (TextView)this.findViewById(R.id.delete_message);
		delete_message.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dBManager = new DBManager(MyMessageDetailActivity.this);
				dBManager.deleteMessage(sqlID);
				dBManager.closeDB();
//				Toast.makeText(MyMessageDetailActivity.this, "成功", 0).show();
				MyMessageDetailActivity.this.finish();
				Intent intent = new Intent();
				intent.putExtra("position", position);
				MyMessageDetailActivity.this.sendBroadcast(intent.setAction("MESSAGE_DELETE"));
			}
		});
		back = (Button)this.findViewById(R.id.header_left);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MyMessageDetailActivity.this.finish();
			}
		});
		
		share = (ImageView)this.findViewById(R.id.share);
		share.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				/*Intent intent = new Intent(MyMessageDetailActivity.this,ShareActivity.class);
				intent.putExtra("title", getIntent().getStringExtra("title"));
				intent.putExtra("pushTime", pushTime.substring(0, index).replaceAll("-", "/"));
				intent.putExtra("content", getIntent().getStringExtra("content"));
				startActivity(intent);*/
				
				Builder build = new AlertDialog.Builder(MyMessageDetailActivity.this).setTitle("分享好友")
				.setItems(R.array.share_item, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(which == 0) {
//							Intent intent = new Intent(Intent.ACTION_MAIN);
//							intent.addCategory(Intent.CATEGORY_DEFAULT);
//							intent.setType("vnd.android-dir/mms-sms");
							Uri uri = Uri.parse("smsto:");
							Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
							intent.putExtra("sms_body", "宽途消息："+getIntent().getStringExtra("title") +";\n时间："+ pushTime.substring(0, index).replaceAll("-", "/")+";\n内容："+getIntent().getStringExtra("content"));
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						} else if(which == 1) {
							Intent intent = new Intent(MyMessageDetailActivity.this, WXEntryActivity.class);
							intent.putExtra("status", 2);
							intent.putExtra("friend", false);
							intent.putExtra("who", ShareActivity.YOU);
							intent.putExtra("title", getIntent().getStringExtra("title"));
							intent.putExtra("pushTime", pushTime.substring(0, index).replaceAll("-", "/"));
							intent.putExtra("content", getIntent().getStringExtra("content"));
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						}else if(which == 2) {
							Intent intent = new Intent(MyMessageDetailActivity.this, WXEntryActivity.class);
							intent.putExtra("status", 2);
							intent.putExtra("friend", true);
							intent.putExtra("who", ShareActivity.YOU);
							intent.putExtra("title", getIntent().getStringExtra("title"));
							intent.putExtra("pushTime", pushTime.substring(0, index).replaceAll("-", "/"));
							intent.putExtra("content", getIntent().getStringExtra("content"));
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						} else if(which == 3) {
							Intent intent = new Intent(MyMessageDetailActivity.this, ShareActivity.class);
							intent.putExtra("status", 0);
							intent.putExtra("who", ShareActivity.YOU);
							intent.putExtra("title", getIntent().getStringExtra("title"));
							intent.putExtra("pushTime", pushTime.substring(0, index).replaceAll("-", "/"));
							intent.putExtra("content", getIntent().getStringExtra("content"));
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						} else if(which == 4) {
							Intent intent = new Intent(MyMessageDetailActivity.this, ShareActivity.class);
							intent.putExtra("status", 1);
							intent.putExtra("who", ShareActivity.YOU);
							intent.putExtra("title", getIntent().getStringExtra("title"));
							intent.putExtra("pushTime", pushTime.substring(0, index).replaceAll("-", "/"));
							intent.putExtra("content", getIntent().getStringExtra("content"));
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						}
					}
				});
				AlertDialog dialog = build.create();
				dialog.show();
				
			}
		});
	}
}
